﻿package org.xhs.report.common.ui.toolbar
{
    import baidu.ui.controls.*;
    import baidu.ui.managers.*;
    import baidu.wenku.common.*;
    import baidu.wenku.common.model.*;
    import baidu.wenku.common.utils.*;
    import flash.display.*;
    import flash.events.*;
    import flash.text.*;
    import flash.ui.*;
    import flash.utils.*;

    public class PageGroup extends Sprite
    {
        protected var pagethTextField:TextField;
        protected var pagesTextField:TextField;
        protected var pageSlashField:TextField;
        protected var pagesTextSpt:Sprite;
        protected var prevBtn:Button;
        protected var nextBtn:Button;
        protected var buttonWidth:Number;
        protected var buttonSpacing:Number = 110;
        protected var errTips:TextField;
        protected var screenModel:ScreenModel;
        protected var locatModel:ILocatModel;
        protected const TIPS_DOC:Object;
        protected const TIPS_TXT:Object;
        public static const PREV_CLICK:String = "PREV_CLICK";
        public static const NEXT_CLICK:String = "NEXT_CLICK";

        public function PageGroup(param1:ILocatModel)
        {
            this.TIPS_DOC = {prev:"上一页", next:"下一页"};
            this.TIPS_TXT = {prev:"上一屏", next:"下一屏"};
            this.locatModel = param1;
            this.addFrame();
            this.addBtns();
            this.addText();
            visible = false;
            y = Config.TB_HEIGHT / 2;
            this.screenModel = InstanceManager.createSingletonInstance(ScreenModel);
            this.screenModel.addEventListener(Event.RESIZE, this.screenResize);
            param1.addEventListener(LocatEvent.ADD_2_SCROLLPANE, this.infoLoaded);
            param1.addEventListener(LocatEvent.PAGETH_CHANGED_PAGEGROUP, this.pagethChanged);
            this.pagethChanged();
            return;
        }// end function

        protected function addFrame() : void
        {
            var _loc_1:* = this.graphics;
            return;
        }// end function

        protected function addBtns() : void
        {
            var _loc_1:* = new AllSkins();
            this.prevBtn = new Button();
            this.prevBtn.setStyle("skin", _loc_1["skin_prev"]);
            this.prevBtn.drawNow();
            this.prevBtn.useHandCursor = true;
            addChild(this.prevBtn);
            this.prevBtn.addEventListener(MouseEvent.CLICK, this.prevBtnClick);
            TooltipManager.getInstance().register(this.prevBtn, Config.isTxt ? (this.TIPS_TXT["prev"]) : (this.TIPS_DOC["prev"]));
            this.prevBtn.enabled = false;
            this.nextBtn = new Button();
            this.nextBtn.setStyle("skin", _loc_1["skin_next"]);
            this.nextBtn.useHandCursor = true;
            addChild(this.nextBtn);
            this.nextBtn.addEventListener(MouseEvent.CLICK, this.nextBtnClick);
            TooltipManager.getInstance().register(this.nextBtn, Config.isTxt ? (this.TIPS_TXT["next"]) : (this.TIPS_DOC["next"]));
            this.nextBtn.enabled = false;
            this.prevBtn.x = 0;
            this.nextBtn.x = this.buttonSpacing;
            var _loc_2:int = 0;
            this.prevBtn.y = 0;
            this.nextBtn.y = _loc_2;
            this.buttonWidth = this.prevBtn.width;
            return;
        }// end function

        protected function addText() : void
        {
            this.pagesTextSpt = new Sprite();
            var _loc_1:* = new TextFormat("Arial", 12);
            _loc_1.align = "center";
            this.pagethTextField = new TextField();
            this.pagethTextField.restrict = "0-9";
            this.pagethTextField.defaultTextFormat = _loc_1;
            this.pagethTextField.type = TextFieldType.INPUT;
            this.pagethTextField.maxChars = 5;
            this.pagethTextField.border = true;
            this.pagethTextField.borderColor = 13754073;
            this.pagethTextField.background = true;
            this.pagethTextField.backgroundColor = 16777215;
            this.pagethTextField.width = 42;
            this.pagethTextField.height = 18;
            this.pagethTextField.text = "0";
            this.pagesTextSpt.addChild(this.pagethTextField);
            this.pagethTextField.addEventListener(KeyboardEvent.KEY_UP, this.pagewordKeyUp);
            this.pagethTextField.addEventListener(FocusEvent.FOCUS_IN, this.pagewordFocusIn);
            this.pagethTextField.addEventListener(FocusEvent.FOCUS_OUT, this.pagewordFocusOut);
            this.pageSlashField = new TextField();
            this.pageSlashField.defaultTextFormat = _loc_1;
            this.pageSlashField.autoSize = "left";
            this.pageSlashField.selectable = false;
            this.pageSlashField.text = "/";
            this.pagesTextSpt.addChild(this.pageSlashField);
            this.pagesTextField = new TextField();
            this.pagesTextField.defaultTextFormat = _loc_1;
            this.pagesTextField.selectable = false;
            this.pagesTextField.autoSize = "left";
            this.pagesTextField.width = 42;
            this.pagesTextField.height = 18;
            this.pagesTextField.text = "0";
            this.pagesTextSpt.addChild(this.pagesTextField);
            addChild(this.pagesTextSpt);
            return;
        }// end function

        protected function pagewordFocusIn(event:FocusEvent = null) : void
        {
            Config.isTextfieldActivated = true;
            return;
        }// end function

        protected function pagewordFocusOut(event:FocusEvent = null) : void
        {
            if (this.locatModel.pagesAllAvailable <= 0)
            {
                this.pagethTextField.text = "0";
                return;
            }
            this.pagethTextField.text = (this.locatModel.pageth + 1) + "";
            Config.isTextfieldActivated = false;
            return;
        }// end function

        protected function pagewordKeyUp(event:KeyboardEvent = null) : void
        {
            var numInput:uint;
            var errtf:TextFormat;
            var id:int;
            var evt:* = event;
            try
            {
                switch(evt.keyCode)
                {
                    case Keyboard.ENTER:
                    case Keyboard.NUMPAD_ENTER:
                    {
                        if (this.pagethTextField.text == "")
                        {
                            this.pagethTextField.text = (this.locatModel.pageth + 1) + "";
                            break;
                        }
                        numInput = uint(this.pagethTextField.text);
                        if (isNaN(numInput) || numInput < 1 || numInput > this.locatModel.pagesAllAvailable)
                        {
                            if (numInput <= this.locatModel.pagesAllTotally && numInput > 0)
                            {
                                this.locatModel.pageth = Config.isTxt ? ((this.locatModel.pagesAllAvailable - 1)) : (this.locatModel.pagesAllAvailable);
                                return;
                            }
                            errtf = new TextFormat("宋体", 12, 16414785);
                            this.errTips = new TextField();
                            this.errTips.defaultTextFormat = errtf;
                            this.errTips.border = true;
                            this.errTips.borderColor = 8363449;
                            this.errTips.background = true;
                            this.errTips.backgroundColor = 16777215;
                            this.errTips.text = "该文档中没有页码为 " + this.pagethTextField.text + " 的页面";
                            this.errTips.autoSize = "left";
                            this.errTips.x = this.pagethTextField.x;
                            this.errTips.y = this.pagethTextField.y + this.pagethTextField.height + 2;
                            addChild(this.errTips);
                            id = setInterval(function () : void
            {
                clearInterval(id);
                if (contains(errTips))
                {
                    removeChild(errTips);
                }
                return;
            }// end function
            , 2000);
                            this.pagethTextField.text = (this.locatModel.pageth + 1) + "";
                        }
                        else
                        {
                            this.locatModel.pageth = numInput - 1;
                        }
                    }
                    default:
                    {
                        break;
                    }
                }
            }
            catch (e:Error)
            {
                Logger.error("pagewordKeyUp, error input...");
            }
            return;
        }// end function

        protected function prevBtnClick(event:MouseEvent = null) : void
        {
            dispatchEvent(new Event(PageGroup.PREV_CLICK));
            return;
        }// end function

        protected function nextBtnClick(event:MouseEvent = null) : void
        {
            dispatchEvent(new Event(PageGroup.NEXT_CLICK));
            return;
        }// end function

        protected function screenResize(event:Event) : void
        {
            if (!(this.screenModel.stageHeight > 0 && this.screenModel.stageWidth > 0))
            {
                return;
            }
            this.reArrange();
            return;
        }// end function

        protected function infoLoaded(event:LocatEvent) : void
        {
            this.prevBtn.enabled = this.locatModel.pageth != 0;
            this.nextBtn.enabled = this.locatModel.pageth != (this.locatModel.pagesAllAvailable - 1);
            this.pagesTextField.text = "" + this.locatModel.pagesAllTotally;
            this.reArrange();
            return;
        }// end function

        protected function pagethChanged(event:LocatEvent = null) : void
        {
            this.pagethTextField.text = (this.locatModel.pageth + 1) + "";
            this.reArrange();
            return;
        }// end function

        public function reArrange() : void
        {
            this.pagesTextSpt.x = 0;
            if (this.errTips && contains(this.errTips))
            {
                removeChild(this.errTips);
            }
            x = Math.min((this.screenModel.stageWidth - width - 24) / 2 + this.buttonWidth / 2, this.screenModel.stageWidth - 188 - width);
            if (this.screenModel.isFullscreen)
            {
                this.pagethTextField.type = TextFieldType.DYNAMIC;
                this.pagethTextField.border = false;
                this.pagethTextField.background = false;
            }
            else if (this.locatModel.pagesAllAvailable > 0)
            {
                this.pagethTextField.type = TextFieldType.INPUT;
                this.pagethTextField.border = true;
                this.pagethTextField.borderColor = 13754073;
                this.pagethTextField.background = true;
                this.pagethTextField.backgroundColor = 16777215;
                x = Math.min((this.screenModel.stageWidth - width - 24) / 2 + this.buttonWidth / 2, this.screenModel.stageWidth - 188 - width);
            }
            this.pagethTextField.x = 0;
            this.pagethTextField.y = (-this.pagethTextField.height) / 2;
            this.pageSlashField.x = this.pagethTextField.x + this.pagethTextField.width;
            this.pageSlashField.y = this.pagethTextField.y;
            this.pagesTextField.x = this.pageSlashField.x + this.pageSlashField.width;
            this.pagesTextField.y = this.pagethTextField.y;
            this.pagesTextSpt.x = (this.buttonSpacing - this.pagesTextSpt.width) / 2;
            return;
        }// end function

        public function setPageArrowAvail(param1:Boolean, param2:Boolean) : void
        {
            var _loc_3:* = param1 ? (this.prevBtn) : (this.nextBtn);
            if (_loc_3)
            {
                _loc_3.enabled = param2;
            }
            return;
        }// end function

        public function disableAll(event:LocatEvent = null) : void
        {
            this.prevBtn.enabled = false;
            this.nextBtn.enabled = false;
            this.pagethTextField.type = TextFieldType.DYNAMIC;
            this.pagethTextField.border = false;
            this.pagethTextField.background = false;
            this.pagethTextField.text = "0";
            this.pagesTextField.text = "0";
            this.reArrange();
            return;
        }// end function

    }
}
